/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package socketchatroom;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Thai
 */
public class User {

    String userName = "";
    String password = "";

    public User(String userName_, String password_) {
        this.userName = userName_;
        this.password = password_;
    }

    /**
     * thêm user mới vào csdl
     *
     * @param userName_ tên đăng nhập
     * @param password_ password
     * @return true nếu thêm thành công, false nếu thêm thất bại
     */
    public boolean addNewUser(String userName_, String password_) {
        File data = new File("data.txt");
        if (!data.exists()) {
            try {
                data.createNewFile();
            } catch (IOException ex) {
                Logger.getLogger(User.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        try {
            if (isUserExit(userName_, password_)) {
                JOptionPane.showMessageDialog(
                        null,
                        "UserName đã tồn tại",
                        "ERROR",
                        JOptionPane.ERROR_MESSAGE);
            }
            FileWriter fw = new FileWriter(data);
            BufferedWriter bufferedWriter = new BufferedWriter(fw);
            bufferedWriter.write(userName_ + "\t" + password_);
            bufferedWriter.flush();
            bufferedWriter.close();
            JOptionPane.showMessageDialog(null, "Thêm thành công");
            return true;
        } catch (IOException ex) {
            JOptionPane.showMessageDialog(null, "Thêm thất bại");
            Logger.getLogger(User.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }

    public boolean isUserExit(String userName_, String password_) {
        File data = new File("data.txt");
        if (!data.exists()) {
            JOptionPane.showMessageDialog(null, "User không tồn tại");
            return false;
        }
        try {
            String content = "";
            BufferedReader reader = new BufferedReader(new FileReader(data));
            while (!(content = reader.readLine()).isEmpty()) {
                String[] subContent = content.split("\t");
                if (subContent[0].equals(userName_)) {
                    if (subContent[1].equals(password_)) {
                        return true;
                    } else {
                        JOptionPane.showMessageDialog(
                                null,
                                "Sai mật khẩu",
                                "ERROR",
                                JOptionPane.ERROR_MESSAGE);
                        return false;
                    }
                } else {
                    JOptionPane.showMessageDialog(null,
                            "Sai tên đăng nhập",
                            "ERROR",
                            JOptionPane.ERROR_MESSAGE);
                }
                try {
                    if (reader != null) {
                        reader.close();
                    }
                } catch (IOException e) {
                }
            }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(User.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(User.class.getName()).log(Level.SEVERE, null, ex);
        }
        return true;
    }
}
